Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
What is a proper way to check if an array is in a nested array? [Javascript]

If I have an array const a = [1, 2] and a nested array const nested = [[1,2], [2,3]]

How do I check if array a is in the nested?

I tried using the nested.includes(a), but it doesn't provide the correct output. And I was thinking stringify the array a, but read some comments about we should not compare array using stringify.

So what is a proper way to check if an array is in another array?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

includes doesn't work correctly as it compares references - a and nested[0] are different in terms of references.

Take a look at following function:

function includesDeep(array, value) {
  return array.some((subArray) => {
    return subArray.every(
      (subArrayElem, index) => subArrayElem === value[index]
    );
  });
}

We have to iterate over all elements of given array and check if every element of this sub array is identical to corresponding element from second array (your a). This method detects type mismatches.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can stringify the arrays to compare whether they are equal in the callback of Array.some :

const a = [1, 2]
const nested = [[1,2], [2,3]]
const containsA = nested.some(e => JSON.stringify(e) == JSON.stringify(a))
console.log(containsA);

about 4 years ago · Juan Pablo Isaza Denunciar

0

if you must compare it without stringification, prepend a unique id to the values and compare that.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda